var floorCount = 21;

var elevator = {
	current: 1,
	status: 0,  // 0: stop, 1: up, 2: down
	task: [],
	nextTask: [],
	doorOpened: false,
	nextUpTask: [],
	nextDownTask: [],
};

$(document).ready(function(){
	var insiderBtnRowAt = floorCount%2;
	$("#well").css("height", floorCount*36+"px");
	$("#insider").css("height", floorCount*36+"px");
	for (var i = floorCount; i > 0; i--) {

		// 渲染楼层上下按键
		var htmlTo = "<div class='btnsRow'><div style='float: left; width: 30px;'>"+i+"</div>";
		if (i != floorCount){
			htmlTo += "<a href='javascript:;'><div class='btn upBtn' floor='"+i+"' acc='1'>▲</div></a>";
		}
		if (i != 1) {
			htmlTo += "<a href='javascript:;'><div class='btn downBtn' floor='"+i+"' acc='2'>▼</div></a>";
		}
		$("#btnsGroup").append(htmlTo+"</div>");

		$("#downer").css("visibility", "hidden");
		$("#uper").css("visibility", "hidden");

		// 渲染电梯内部按键
		if (i % 2 == insiderBtnRowAt) {
			$("#insider").append("<div class='btnsRow'><a href='javascript:;'><div class='btn' floor='"+i+"' acc='0'>"+i+"</div></a>");
			if (i-1 != 0) {
				$("#insider").append("<a href='javascript:;'><div class='btn' floor='"+(i-1)+"' acc='0'>"+(i-1)+"</div></a>");
			}
			$("#insider").append("</div>");
		}

	}
	$(".btn[acc='0']").click(function(){
		$(this).css("color", "red");
		addTask($(this).attr("floor"));
	});

	$(".btn[acc='1']").click(function(){
		$(this).css("color", "red");
		addUpTask($(this).attr("floor"));
	});
	$(".btn[acc='2']").click(function(){
		$(this).css("color", "red");
		addDownTask($(this).attr("floor"));
	});

	// 初始化电梯状态
	$("#elevator").css("top", (floorCount - elevator.current)*36 + "px");
});

function addTask(floor){
	if (elevator.current < floor) {
		if (elevator.status == 0) {
			elevator.task.push(floor);
			elevator.task.sort(sortFloor);
			moveUp();
		}else if (elevator.status == 1) {
			elevator.task.push(floor);
			elevator.task.sort(sortFloor);
		}else if (elevator.status == 2) {
			elevator.nextTask.push(floor);
		}
	}else if (elevator.current > floor) {
		if (elevator.status == 0) {
			elevator.task.push(floor);
			elevator.task.sort(sortFloor);
			moveDown();
		}else if (elevator.status == 1) {
			elevator.nextTask.push(floor);
		}else if (elevator.status == 2) {
			elevator.task.push(floor);
			elevator.task.sort(sortFloor);
		}
	}else{
		if (!elevator.doorOpened) {
			if (elevator.status == 0) {
				doorOpen(floor, function(){
				});
			}else{
				elevator.nextTask.push(floor);
			}
		}else{
			$(".btn[floor='"+floor+"']").css("color", "black");
		}
	}
	printTask();
}

function addUpTask(floor){
	if (elevator.current < floor) {
		if (elevator.status == 0) {
			elevator.task.push(floor);
			elevator.task.sort(sortFloor);
			moveUp();
		}else if (elevator.status == 1) {
			elevator.task.push(floor);
			elevator.task.sort(sortFloor);
		}else if (elevator.status == 2) {
			elevator.nextTask.push(floor);
		}
	}else if (elevator.current > floor) {
		if (elevator.status == 0) {
			elevator.task.push(floor);
			elevator.task.sort(sortFloor);
			moveDown();
		}else if (elevator.status == 1) {
			elevator.nextTask.push(floor);
		}else if (elevator.status == 2) {
			elevator.nextTask.push(floor);
		}
	}else{
		if (!elevator.doorOpened) {
			if (elevator.status == 0) {
				elevator.status = 1;
				$("#uper").css("visibility", "visible");
				$("#downer").css("visibility", "hidden");
				doorOpen(floor, function(){
					moveStop();
				});
			}else{
				elevator.nextTask.push(floor);
			}
		}else{
			$(".btn[floor='"+floor+"']").css("color", "black");
		}
		
	}
}

function addDownTask(floor){
	if (elevator.current < floor) {
		if (elevator.status == 0) {
			elevator.task.push(floor);
			elevator.task.sort(sortFloor);
			moveUp();
		}else if (elevator.status == 1) {
			elevator.nextTask.push(floor);
		}else if (elevator.status == 2) {
			elevator.nextTask.push(floor);
		}
	}else if (elevator.current > floor) {
		if (elevator.status == 0) {
			elevator.task.push(floor);
			elevator.task.sort(sortFloor);
			moveDown();
		}else if (elevator.status == 1) {
			elevator.nextTask.push(floor);
		}else if (elevator.status == 2) {
			elevator.task.push(floor);
			elevator.task.sort(sortFloor);
		}
	}else{
		if (!elevator.doorOpened) {
			if (elevator.status == 0) {
				elevator.status = 2;
				$("#uper").css("visibility", "hidden");
				$("#downer").css("visibility", "visible");
				doorOpen(floor, function(){
					moveStop();
				});
			}else{
				elevator.nextTask.push(floor);
			}
		}else{
			$(".btn[floor='"+floor+"']").css("color", "black");
		}
		
	}
}

function moveUp(){
	printTask();
	elevator.status = 1;
	$("#uper").css("visibility", "visible");
	$("#downer").css("visibility", "hidden");

	$("#elevator").animate({
		top: '-=36px'
	}, 1000, function(){
		elevator.current ++;
		$("#floorNum").html(elevator.current);
		if (elevator.current == elevator.task[0]) {
			doorOpen(elevator.task[0], function(){
				elevator.task.shift();
				elevator.task.sort(sortFloor);
				if (elevator.task.length == 0) {
					elevator.task = elevator.nextTask;
					elevator.task.sort(sortFloor);
					elevator.nextTask = [];
					if (elevator.task.length == 0) {
						moveStop();
					}else{
						// elevator.task.concat(elevator.nextDownTask);
						moveDown();
					}
				}else{
					moveUp();
				}
			});
		}else{
			if (elevator.current < floorCount) {
				moveUp();
			}else{
				moveDown();
			}
		}
	});
}

function moveDown(){
	printTask();
	elevator.status = 2;
	$("#downer").css("visibility", "visible");
	$("#uper").css("visibility", "hidden");

	$("#elevator").animate({
		top: '+=36px'
	}, 1000, function(){
		elevator.current --;
		$("#floorNum").html(elevator.current);

		if (elevator.current == elevator.task[0]) {
			doorOpen(elevator.task[0], function(){
				elevator.task.shift();
				
				if (elevator.task.length == 0) {
					elevator.task = elevator.nextTask;
					elevator.nextTask = [];
					if (elevator.task.length == 0) {
						moveStop();
					}else{
						moveUp();
					}
				}else{
					moveDown();
				}
			});
		}else{
			if (elevator.current > 1) {
				moveDown();
			}else{
				moveUp();
			}
		}
	});
}

function moveStop(){
	elevator.status = 0;
	$("#downer").css("visibility", "hidden");
	$("#uper").css("visibility", "hidden");
}

function doorOpen(floor ,finish){
	$(".btn[floor='"+floor+"']").css("color", "black");
	$("#elevator").css("background", "#ddd");
	elevator.doorOpened = true;
	setTimeout(function() {
		$("#elevator").css("background", "#ccc");
		elevator.doorOpened = false;
		finish();
	}, 3000);
}
function sortFloor(a, b){
	return Math.abs(a - elevator.current) - Math.abs(b - elevator.current);
}
function printTask(){
	console.log("task: "+elevator.task);
	console.log("nextTask: "+elevator.nextTask);
}